package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.domain.TenantMeal;
import cn.itsource.hrm.domain.vo.TenantVo;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.mapper.TenantMealMapper;
import cn.itsource.hrm.query.TenantQuery;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.PageList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinhui
 * @since 2020-05-27
 */
@Service
@Transactional
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private TenantMealMapper tenantMealMapper;
    @Override
    public PageList<Tenant> pagelist(TenantQuery query) {
        IPage<Tenant> iPage = baseMapper.selectPage(new Page<Tenant>(query.getPage(), query.getRows()), query);
        return new PageList<>(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public void register(TenantVo tenantVo) {
        //租户表中添加数据
        Tenant tenant = new Tenant();
        BeanUtils.copyProperties(tenantVo,tenant);
        //设置租户基本熟悉
        tenant.setRegisterTime(System.currentTimeMillis());
        tenant.setState(0);
        baseMapper.insert(tenant);
        //员工表中添加数据
        Employee employee = new Employee();
        BeanUtils.copyProperties(tenantVo,employee);
        //设置员工基本属性
        employee.setInputTime(System.currentTimeMillis());
        //mybatisplus默认返回主键
        employee.setTenantId(tenant.getId());
        employee.setState(1);
        employeeMapper.insert(employee);
        //租户套餐中间表中添加数据
        TenantMeal tenantMeal = new TenantMeal();
        tenantMeal.setMealId(tenantVo.getMeal());
        tenantMeal.setTenantId(tenant.getId());
        tenantMealMapper.insert(tenantMeal);
    }
}
